/*
 * Copyright (c) 2008, Takashi Ito
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the authors nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _TANSU_NWIF_H_
#define _TANSU_NWIF_H_

#include "tansu_status.h"

#ifdef __cplusplus
extern "C" {
#endif

#define TANSU_MAC_ADDR_LEN_MAX      ((2 * 6) + (1 * 5))
#define TANSU_IPv4_ADDR_LEN_MAX     16
#define TANSU_IPv6_ADDR_LEN_MAX     46

#define TANSU_IPv4_ANYADDR          "0.0.0.0"
#define TANSU_IPv4_LOOPBACK         "127.0.0.1"

#define TANSU_NWIF_UP               (1 << 0)
#define TANSU_NWIF_RUNNING          (1 << 1)
#define TANSU_NWIF_LOOPBACK         (1 << 2)

#define NWIF_IS_ACTIVE(flags) \
    (((flags) & (TANSU_NWIF_UP | TANSU_NWIF_RUNNING)) ? 1 : 0)

/**
 * Type for network interface.
 */
struct tansu_nwif {
    STAILQ_ENTRY(tansu_nwif) entry;
    char *name;
    int family;
    char *addr;
    char *netmask;
    unsigned char hwaddr[6];
    uint16_t flags;
};
STAILQ_HEAD(tansu_nwifs, tansu_nwif);

/**
 * Creates a network interface instance.
 *
 * @return an instance or NULL
 */
struct tansu_nwif *tansu_nwif_new();

/**
 * Gets all interface information.
 *
 * @param[out] status
 *
 * @return interface list.
 */
struct tansu_nwifs *tansu_nwif_getaddrs(tansu_status_t *status);

/**
 * Destructs the network interface instance.
 *
 * @param[in] nwif instance to be deleted.
 */
void tansu_nwif_delete(struct tansu_nwif *nwif);

/**
 * Destructor.
 *
 * @param[in] head instances to be deleted.
 */
void tansu_nwif_freeaddrs(struct tansu_nwifs *head);


/**
 * Type for network interface event.
 */
struct tansu_nwif_event {
    STAILQ_ENTRY(tansu_nwif_event) entry;
    uint32_t event;
    struct tansu_nwif *before;
    struct tansu_nwif *after;
};
STAILQ_HEAD(tansu_nwif_events, tansu_nwif_event);

#define TANSU_NWIF_EVENT_IF_ADDED       (1 << 0)
#define TANSU_NWIF_EVENT_IF_REMOVED     (1 << 1)
#define TANSU_NWIF_EVENT_ADDR_CHANGED   (1 << 2)
#define TANSU_NWIF_EVENT_FLAG_CHANGED   (1 << 3)

/**
 * Deletes all nwif events.
 *
 * @param[in] events event list to be deleted.
 */
void tansu_nwif_events_delete(struct tansu_nwif_events *events);

/**
 * Compares the two instances.
 */
uint32_t
tansu_nwif_compare(struct tansu_nwif *before,
                   struct tansu_nwif *after);

/**
 * Type for network interface listener.
 */
#define TANSU_NWIF_EVENT_FUNC(name) \
    void (name)(struct tansu_nwif_events *events, void *userdata)
struct tansu_nwif_listener {
    STAILQ_ENTRY(tansu_nwif_listener) entry;
    TANSU_NWIF_EVENT_FUNC(*callback);
    void *userdata;
};
STAILQ_HEAD(tansu_nwif_listeners, tansu_nwif_listener);

/**
 * Type for network interface monitor.
 */
struct tansu_nwif_monitor {
    struct event ev;
    struct timeval interval;
    struct tansu_nwifs *latest;
    struct tansu_nwif_listeners listeners;
};

/**
 * Creates a new network interface monitor instance.
 *
 * @return an instance or NULL
 */
struct tansu_nwif_monitor * tansu_nwif_monitor_new();

/**
 * Destructs the network interface monitor instance.
 *
 * @param[in] monitor instance to be deleted.
 */
void
tansu_nwif_monitor_delete(struct tansu_nwif_monitor *monitor);

/**
 * Adds a callback.
 */
struct tansu_nwif_listener *
tansu_nwif_monitor_add(struct tansu_nwif_monitor *monitor,
                       TANSU_NWIF_EVENT_FUNC(*callback),
                       void *userdata);

/**
 * Removes the callback.
 */
void
tansu_nwif_monitor_remove(struct tansu_nwif_monitor *monitor,
                          struct tansu_nwif_listener *listener);

/**
 * Polls the network interfaces and notifies the changes to listeners
 */
void
tansu_nwif_monitor_poll(struct tansu_nwif_monitor *monitor);

#ifdef __cplusplus
}
#endif

#endif /* _TANSU_NWIF_H_ */

